/*   Copyright 2010 Jonathan Wagner
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.alienos.collide.bounds;

import static java.lang.Math.*;

import com.alienos.collide.math.Vector3;

public class BoundingBox extends BoundingVolume {

    private final Vector3 center;
    private final Vector3 halfExtents;

    public BoundingBox(Vector3 center, Vector3 halfExtents) {
        this.center = center;
        this.halfExtents = halfExtents;
    }

    public BoundingBox(Vector3 center, float xExtent, float yExtent, float zExtent) {
        this.center = center;
        this.halfExtents = new Vector3(xExtent, yExtent, zExtent);
    }

    public Vector3 getCenter() {
        return center;
    }

    public Vector3 getHalfExtents() {
        return halfExtents;
    }

    @Override
    public boolean intersects(BoundingVolume v) {

        if (null != v) {
            return v.intersects(this);
        } else {
            return false;
        }
    }

    @Override
    public boolean intersects(BoundingBox box) {

        Vector3 thatCenter = box.getCenter();
        Vector3 thatExtents = box.getHalfExtents();

        if (abs(center.getX() - thatCenter.getX()) > (halfExtents.getX() + thatExtents.getX())) {
            return false;
        }
        if (abs(center.getY() - thatCenter.getY()) > (halfExtents.getY() + thatExtents.getY())) {
            return false;
        }
        if (abs(center.getZ() - thatCenter.getZ()) > (halfExtents.getZ() + thatExtents.getZ())) {
            return false;
        }

        return true;
    }

    @Override
    public boolean intersects(BoundingSphere sphere) {
        return intersectSphereAABB(sphere, this);
    }

    public static float SquareDistance(Vector3 point, BoundingBox b) {
        float sq = 0.0f;

        return sq;
    }

    @Override
    public boolean contains(Vector3 point) {
        return abs(center.getX() - point.getX()) < halfExtents.getX()
                && Math.abs(center.getY() - point.getY()) < halfExtents.getY()
                && Math.abs(center.getZ() - point.getZ()) < halfExtents.getZ();
    }

}
